﻿using System;
using System.Collections.Generic;
using System.Globalization;
namespace Homework13
{
     class Program
    {/// <summary>
    /// 随机生成10000个数据，由姓名，年龄，生日，身高，性别组成；
    /// 计算男女各自的平均年龄，平均身高
    /// </summary>
    /// <param name="args"></param>
        static void Main()
        {//建立随机，储存数组
            Random random = new Random();
            List<Person> people = new List<Person>();
            for (int i = 0; i < 10000; i++) 
            {//定义
                string name=GenerateName(random);
                int age=GenerateAge(random);
                DateTime birthday = GenerateBirthday(random, age);
                double height = GenerateHeight(random);
                string gender = GenerateGender(random);
                Person person = new Person
                {
                    Name = name,
                    Age = age,
                    Birthday = birthday,
                    Height = height,
                    Gender = gender
                };

                people.Add(person);
            }// 输出每个人员记录  
            foreach (Person person in people)
            {
                Console.WriteLine($"{person.Name},{person.Age},{person.Birthday.ToShortDateString()},{person.Height},{person.Gender}");
            }
            // 计算平均身高和年龄  
            double maleAverageHeight = 0;
            double femaleAverageHeight = 0;
            double maleAverageAge = 0;
            double femaleAverageAge = 0;
            int maleCount = 0;
            int femaleCount = 0;
            foreach (Person person in people)
            {
                if (person.Gender == "MALE")
                {
                    maleAverageHeight += person.Height;
                    maleAverageAge += person.Age;
                    maleCount++;
                }
                else if (person.Gender == "FEMALE")
                {
                    femaleAverageHeight += person.Height;
                    femaleAverageAge += person.Age;
                    femaleCount++;
                }
            }
            // 输出平均结果  
            Console.WriteLine("\nAverage Height and Age by Gender:");
            Console.WriteLine($"Male Average Height: {maleAverageHeight / maleCount:F2}");
            Console.WriteLine($"Male Average Age: {maleAverageAge / maleCount:F0}");
            Console.WriteLine($"Female Average Height: {femaleAverageHeight / femaleCount:F2}");
            Console.WriteLine($"Female Average Age: {femaleAverageAge / femaleCount:F0}");
        }
        static string GenerateName(Random random)
        {//由3到6个字母组成，首字母大写
            int length = random.Next(3, 7);
            char firstLetter = (char)random.Next('A', 'Z');
            char[] nameChars = new char[length - 1];
            for (int i = 0; i < nameChars.Length; i++)
            {
                nameChars[i] = (char)random.Next('a', 'z');
            }
            return new string(new char[] { firstLetter }.Concat(nameChars).ToArray());
        }
        static int GenerateAge(Random random) 
        {//年龄在20-60之间
            return random.Next(20, 61);
        }
        static DateTime GenerateBirthday(Random random, int age)
        {
            DateTime now = DateTime.Now;
            int startYear = now.Year - age;
            int birthYear = random.Next(startYear, startYear + 1);
            int birthMonth = random.Next(1, 13);
            int birthDay = random.Next(1, DateTime.DaysInMonth(birthYear, birthMonth) + 1);
            return new DateTime(birthYear, birthMonth, birthDay);
        }
        static double GenerateHeight(Random random)
        {//身高在1.5-1.9之间，保留两位小数
            return Math.Round(random.NextDouble() * (1.9 - 1.5) + 1.5, 2, MidpointRounding.AwayFromZero);  
        }

        static string GenerateGender(Random random)
        {//随机分配性别
            return random.Next(2) == 0 ? "MALE" : "FEMALE";
        }
        class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public DateTime Birthday { get; set; }
            public double Height { get; set; }
            public string Gender { get; set; }
        }

    }
}
